namespace RushMail.Requests;

/// <summary>
/// 帐号相关
/// </summary>
public class Account
{
    private readonly IRushMailClient _client;

    public Account(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 获取资金账户信息
    /// http://edmapi.rushmail.com/api.php?module=fund&action=fund-detail
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<FundDetailResponse>> FundDetail() =>
        (await _client.PostAsync(new RushMailRequest("fund", "fund-detail"))).ToGeneric<FundDetailResponse>();
    
    /// <summary>
    /// 子帐号列表
    /// http://edmapi.rushmail.com/api.php?module=account&action=list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<AccountListResponse>> List() =>
        (await _client.PostAsync(new RushMailRequest("account", "list"))).ToGeneric<AccountListResponse>();
    
    /// <summary>
    /// 添加子帐号
    /// http://edmapi.rushmail.com/api.php?module=account&action=add
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<int>> Add(AccountAdd body) =>
        (await _client.PostAsync(new RushMailRequest("account", "add")
        {
            Body = body.ToDictionary()
        }))
        .ToGeneric<int>();
    
    /// <summary>
    /// 修改子帐号
    /// http://edmapi.rushmail.com/api.php?module=account&action=edit
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Edit(AccountEdit body) =>
        _client.PostAsync(new RushMailRequest("account", "edit")
        {
            Body = body.ToDictionary()
        });
    
    /// <summary>
    /// 删除子帐号
    /// http://edmapi.rushmail.com/api.php?module=account&action=delete
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> Delete(int accountId) =>
        _client.PostAsync(new RushMailRequest("account", "delete")
        {
            Body = new Dictionary<string, string>() { {"account_id", accountId.ToString()} }
        });
}

public class FundDetailResponse
{
    public Fund Fund { get; set; }
}

public class Fund
{
    [JsonProperty("total_spending")]public int TotalSpending { get; set; }
    public int Balance { get; set; }
}

public class AccountListResponse
{
    [JsonProperty("account_list")]public List<AccountItem> AccountList { get; set; }
}

public class AccountItem
{
    [JsonProperty("account_id")]public int AccountId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Admin { get; set; }
    public int Status { get; set; }
}

public class AccountAdd
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public int Admin { get; set; }
    public int Status { get; set; }

    public virtual Dictionary<string, string> ToDictionary() => new Dictionary<string, string>()
    {
        { "name", Name },
        { "email", Email },
        { "password", Password },
        { "admin", Admin.ToString() },
        { "status", Status.ToString() },
    };
}

public class AccountEdit : AccountAdd
{
    public int AccountId { get; set; }

    public override Dictionary<string, string> ToDictionary()
    {
        var dic = base.ToDictionary();
        dic.Add("account_id", AccountId.ToString());
        return dic;
    }
}